import { set, cloneDeep } from 'lodash'
const chargesList = (itemIds, prices) => {
  return prices.filter(i => itemIds.indexOf(i.otherId) !== -1)
}

const getItem = (itemIds, item) => {
  const clone = cloneDeep(item)
  const charges = chargesList(itemIds, item.chargebeeItemPricesList || [])
  if (charges.length) {
    set(clone, 'chargebeeItemPricesList', charges)
  } else {
    set(clone, 'chargebeeItemPricesList', [])
  }
  return clone
}

export default (itemIds, items) => {
  return items.reduce((result, record) => {
    const item = getItem(itemIds, record)
    if (item.chargebeeItemPricesList && item.chargebeeItemPricesList.length) {
      result.push(item)
    }

    return result
  }, [])
}
